{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e2cc6eb4",
   "metadata": {},
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb4a2c42",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8e90fe29",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd modelscope-agent && pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9794ba6c-fa1a-43f2-a2a6-86f35fec0ba1",
   "metadata": {},
   "source": [
    "### 下载本地语料库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96e36b08-5ab8-4905-aaea-15621039a951",
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget -P modelscope-agent/demo/ https://modelscope.oss-cn-beijing.aliyuncs.com/resource/agent/modelscope_qa.txt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef9ccb1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ad42f062-5664-44f8-82b7-6995a4f512a0",
   "metadata": {},
   "source": [
    "### 读取环境变量文件和工具config配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f06ad1-34c5-4032-9254-aab88d7fe19d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from modelscope.utils.config import Config\n",
    "\n",
    "# 配置环境变量，里面需要配置modelscope token来remote调用AI模型\n",
    "os.environ['TOOL_CONFIG_FILE'] ='../config/cfg_tool_template.json'\n",
    "os.environ['OUTPUT_FILE_DIRECTORY'] = '/tmp'\n",
    "\n",
    "# ModelScope Token从这里获取: https://modelscope.cn/my/myaccesstoken\n",
    "print('请输入ModelScope Token，可以从这里获取: https://modelscope.cn/my/myaccesstoken')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()\n",
    "\n",
    "# 读取工具调用CONFIG文件，包括工具的名称和远程请求url\n",
    "tool_cfg_file = os.getenv('TOOL_CONFIG_FILE')\n",
    "print(tool_cfg_file)\n",
    "tool_cfg = Config.from_file(tool_cfg_file)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "71a5f182-f8a1-497e-b860-b442d6106126",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 中枢大模型启动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbbee3d8-067b-405d-b630-2a74d4e78b5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.llm import LLMFactory\n",
    "from modelscope_agent.agent import AgentExecutor\n",
    "\n",
    "\n",
    "model_name = 'modelscope-agent-7b'\n",
    "model_cfg = {\n",
    "    'modelscope-agent-7b':{\n",
    "        'type': 'modelscope',\n",
    "        'model_id': 'damo/ModelScope-Agent-7B',\n",
    "        'model_revision': 'v1.0.0',\n",
    "        'use_raw_generation_config': True,\n",
    "        'custom_chat': True\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6a2c55c7-8b24-4d48-ade6-8e1e80760172",
   "metadata": {},
   "source": [
    "### 向量库配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cada29c-1892-4ec9-901f-615a3c5da0e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings import ModelScopeEmbeddings, DashScopeEmbeddings\n",
    "from langchain.vectorstores import FAISS\n",
    "from modelscope_agent.retrieve import ToolRetrieval, KnowledgeRetrieval\n",
    "import faiss\n",
    "\n",
    "# 开源版本的向量库配置\n",
    "model_id = 'damo/nlp_corom_sentence-embedding_chinese-base'\n",
    "embeddings = ModelScopeEmbeddings(model_id=model_id)\n",
    "\n",
    "# 基于阿里云DashScope向量Embedding的配置\n",
    "#embeddings = DashScopeEmbeddings(model=\"text-embedding-v1\")\n",
    "\n",
    "\n",
    "file_path = 'modelscope_qa.txt'\n",
    "knowledge_retrieval = KnowledgeRetrieval.from_file(file_path, embeddings, FAISS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6590e60b-f483-43c9-94c1-7f55a9b81d10",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = AgentExecutor(llm, tool_cfg, knowledge_retrieval=knowledge_retrieval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a318c3d-dc1e-4d53-b973-4350d4d27a67",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.run('pip install的时候有些包下载特别慢怎么办')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "651e7092-000c-48e8-911c-1ab926084f92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
